<?php 
/**
 * 
 * @author tangbl
 *
 */
class NodeModel extends Model {

	/*array(
		0.字段名   
		1.验证规则 [require 字段必须、email 邮箱、url URL地址、currency 货币、number 数字]
		2.提示信息
		3.验证条件 [0.(默认)存在字段就验证;1.必须验证;2.值不为空的时候验证]
		4.附加规则 [regex、function、callback、confirm、equal、in、length、between、expire、ip_allow、ip_deny、unique]
		5.验证时   [1.新增数据时候验证;2.编辑数据时候验证;3.(默认)全部情况下验证]
	);*/
	protected $_validate=array(
		//array('title','require','{%nod_form_validata_title_required}',1,'',3),
		array('name','require','{%nod_form_validata_name_required}',1,'',1),
		array('level',array(0,1,2,3),'{%nod_form_validata_level_required}',1,'in',3),
		array('permiss',array(1,2),'{%nod_form_validata_permiss_req}',1,'in',3),
	);

	// 获取所有节点信息
	public function getAllNode($where = '' , $order = 'sort DESC') {
		return $this->where($where)->order($order)->select();
	}

	// 获取单个节点信息
	public function getNode($where = '',$field = '*') {
		return $this->field($field)->where($where)->find();
	}

	// 删除节点
	public function delNode($where) {
		if($where){
			return $this->where($where)->delete();
		}else{
			return false;
		}
	}

	// 更新节点
	public function upNode($data) {
		if($data){
			return $this->save($data);
		}else{
			return false;
		}
	}

	// 子节点
	public function childNode($id){
		return $this->where(array('pid'=>$id))->select();
	}
	
	//生成option的html
	public function getAblSelectNode($all,$show,$default){
		$html = '';
		foreach($all as $v){
		    $select = $v==$default ? "selected" : "";
			if(!in_array($v,$show)||($v==$default)){
				$html .= '<option value="'.$v.'" '.$select.'>'.$v.'</option>';
			}else{
				$html .= '<option value="'.$v.'" '.$select.' disabled>'.$v.'</option>';
			}
		}
		return $html;
	}
	
	//得到可选的模块
	public function getModuleNode($default=''){
		/*
		$list     = $this->where("level=2")->group("name")->field("name")->select();
		$exsits   = array();
		foreach($list as $v){
			$exsits[] = $v['name'];
		}*/
		$noexsits = $this->getModule(array());
		return $this->getAblSelectNode($noexsits,array(),$default);
	}
	
	//得到可选的方法
	public function geActionNode($name,$default=''){
		$sql      = $this->where("name='$name' and level=1")->field("id")->select(false);
		$list     = $this->where("pid in ".$sql)->field("name")->select();
		$exsits   = array();
		foreach($list as $v){
			$exsits[] = $v['name'];
		}
		
		$noexsits = $this->getFunctions($name);
		return $this->getAblSelectNode($noexsits,$exsits,$default);
	}
	
	//得到所有Action的名称
	public function getModule($exsits=array('Public','Common','Test','Empty')){
        $group_path   = LIB_PATH.'Action';

        $group_path   .= '/*.class.php';
        $ary_files     = glob($group_path);
		//$exsits        = array('Public','Common','Test','Empty');

        foreach ($ary_files as $file) {
            if (is_dir($file)) {
                continue;
            }else {
				$name    = basename($file,'Action.class.php');
				if(!in_array($name,$exsits))	
                $files[] = $name;
            }
        }
        $module = F('Module');
        if(is_array($module)){
        	$files  = array_merge($module,$files);
        }
       	 
        return $files;
    }
	
	//得到对应Action的所有自定义方法
    public function getFunctions($module){
    	
    	$modules = F('Module');
    	if(in_array($module,$modules)){
    		$module = "Article";
    	}

    	$action 	= A($module);
        $functions  = get_class_methods($action);
        $exsits     = array(
            '_initialize','__construct','getActionName','isAjax','display','show','fetch',
            'buildHtml','assign','__set','get','__get','__isset','_initmenu','_empty',
            '__call','error','success','ajaxReturn','redirect','__destruct'
        );
        
		$node_funcs = array();
        foreach ($functions as $func){
            if(!in_array($func, $exsits)){
                $node_funcs[]=$func;
            }
        }
        return $node_funcs;
    }
	
	//得到已经存在的node方法
	public function getExsitsFun($param){
		$list = $this->where($param)->field("id")->select();
		
		$func = array();
		foreach($list as $v){
			$rows = $this->childNode($v['id']);	
			foreach($rows as $k){
				$func[] = $k['name'];
			}
		}
		return $func;
	}
	
	public function create_child_node($func,$lanVal,$obj,$lang){
		
		if(is_array($func)){
			$data['pid']      = $obj['id'];
			$data['level']    = 2;
			$data['permiss']  = $obj['permiss'];
			$data['dengji']   = 3;
			$data['status']   = $obj['status'];
			$data['icon']     = $obj['icon'];
			$data['title']    = $obj['remark'];
			foreach($func as $v){
				$data['name']  = $v;
				$data['title'] = $v;
				$data['data']  = $obj['name']."/".$v;
				
				$row = $lanVal[$v];
				if(empty($row)){
					foreach($lang as $k=>$a){
						$row[$k] = $v;
					}
				}
				$data['langval'] = json_encode($row);
				$this->add($data);
			}	
		}
	}
}